<?php

class AuthorityRepository {

    var $db;

    public function AuthorityRepository() {
        global $SERVER, $DATABASE, $USERNAME, $PASSWORD;
        $this->db = new Connection($SERVER, $DATABASE, $USERNAME, $PASSWORD);
    }

    public function GetAll() {
        $query = "SELECT * FROM authority";
        $result = $this->db->ExecuteQuery($query);
        return $this->ConvertToList($result);
    }

    public function ConvertToList($result) {
        $list = array();
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $list[] = new AuthorityEntity($row[0], $row[1], $row[2]);
        }
        return $list;
    }

    public function GetAuthoritiesByGroup($groupId) {
        $query = "SELECT * FROM authority WHERE ID_i in " .
                "(SELECT AuthorityID_i FROM group_authority WHERE GroupID_i = " . $groupId . ")";
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error: " . mysql_error());
        return $this->ConvertToList($result);
    }

    public function DeleteGroupAuthority($groupID, $authorityID) {
        $query = "DELETE FROM `group_authority` WHERE AuthorityID_i = " . $authorityID . " AND GroupID_i = " . $groupID;
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error:" . mysql_error());
        if ($result != 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function InsertGroupAuthority($groupID, $authorityID) {
        $query = "INSERT INTO `group_authority` (GroupID_i,AuthorityID_i) VALUES (" . $groupID . "," . $authorityID . ")";
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error:" . mysql_error());
        if ($result != 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function GetGroupAuthoritiesByGroupId($groupId) {
        $query = "SELECT * FROM `group_authority` WHERE GroupID_i=" . $groupId;
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error:" . mysql_error());
        $list = array();
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $list[] = new GroupAuthorityEntity($row[0], $row[1], $row[2]);
        }
        return $list;
    }

    public function UpdateGroupAu($groupID, array $list) {
        mysql_query('BEGIN');
        $result = $this->GetGroupAuthoritiesByGroupId($groupID);
        if (count($result) > 0) {
            for ($i = 0; $i < count($result); $i++) {
                if (!$this->DeleteGroupAuthority($groupID, $result[$i]->AuthorityID_i)) {
                    mysql_query("ROLLBACK");
                    echo "false to delete";
                    return FALSE;
                }
            }

            for ($i = 0; $i < count($list); $i++) {
                if (!$this->InsertGroupAuthority($groupID, $list[$i])) {
                    mysql_query("ROLLBACK");
                    echo "false to insert";
                    return FALSE;
                }
            }

            mysql_query("COMMIT");
            return TRUE;
        } else {
            for ($i = 0; $i < count($list); $i++) {
                if (!$this->InsertGroupAuthority($groupID, $list[$i])) {
                    mysql_query("ROLLBACK");
                    echo "false to insert";
                    return FALSE;
                }
            }

            mysql_query("COMMIT");
            return TRUE;
        }
    }

}

?>
